package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.bo.WbDemandListBo;
import org.dromara.system.domain.vo.SysUserImportVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.domain.vo.WbDemandListVo;
import org.dromara.system.service.ISysConfigService;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.IWbDemandListService;

import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbDemandListImportListener extends AnalysisEventListener<WbDemandListVo> implements ExcelListener<WbDemandListVo> {

    private final IWbDemandListService wbDemandListService;

    private final String itemId;

    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbDemandListImportListener(String itemId) {
        this.wbDemandListService = SpringUtils.getBean(IWbDemandListService.class);
        this.itemId = itemId;
    }

    @Override
    public void invoke(WbDemandListVo wbDemandListVo, AnalysisContext context) {
        try {
            if(StringUtils.isNotEmpty(itemId) && StringUtils.isNotEmpty(wbDemandListVo.getSubStandardId())){
                WbDemandListBo wbDemandListBo = BeanUtil.toBean(wbDemandListVo, WbDemandListBo.class);
                wbDemandListBo.setItemId(itemId);
                WbDemandListBo wbDemandListBo1 = new WbDemandListBo();
                wbDemandListBo1.setItemId(itemId);
                if (StringUtils.isNotEmpty(wbDemandListBo.getSubStandardId())){
                    wbDemandListBo1.setSubStandardId(wbDemandListBo.getSubStandardId());
                }
                if (StringUtils.isNotEmpty(wbDemandListBo.getSubStandardName())){
                    wbDemandListBo1.setSubStandardName(wbDemandListBo.getSubStandardName());
                }
                if (StringUtils.isNotEmpty(wbDemandListBo.getBagId())){
                    wbDemandListBo1.setBagId(wbDemandListBo.getBagId());
                }
                List<WbDemandListVo> wbDemandListVos = wbDemandListService.queryList1(wbDemandListBo1);
                if (wbDemandListVos.size() <= 0) {
                    wbDemandListService.insertByBo(wbDemandListBo);
                    successNum++;
                }else {
                    Long id = wbDemandListVos.get(0).getId();
                    wbDemandListBo.setId(id);
                    wbDemandListService.updateByBo(wbDemandListBo);
                    updateNum++;
                }
                successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbDemandListVo.getSubStandardId()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbDemandListVo.getSubStandardId() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbDemandListVo.getSubStandardId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbDemandListVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + "，修改共，"+ updateNum +"条， 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbDemandListVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
